/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.loaiSanPhamPOJO;
import com.mysql.jdbc.CallableStatement;
import dbhelper.dbconnect;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author SoftVnn Member
 */
public class loaiSanPhamDAO {
    public ArrayList<loaiSanPhamPOJO> getAll()
    {
        ArrayList<loaiSanPhamPOJO> arr = new ArrayList<loaiSanPhamPOJO>();
        try
        {
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spLoaiSanPham_getAll();");
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {
                loaiSanPhamPOJO obj = new loaiSanPhamPOJO();
                obj.setMaNhom(rs.getString("MaNhom"));
                obj.setMaLoaiSanPham(rs.getString("MaLoaiSanPham"));
                obj.setTenLoaiSanPham(rs.getString("TenLoaiSanPham"));
                arr.add(obj);
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error;
        }
        return arr;
    }
    public ArrayList<loaiSanPhamPOJO> get(String MaLoaiSanPham)
    {
        ArrayList<loaiSanPhamPOJO> arr = new ArrayList<loaiSanPhamPOJO>();
        try
        {           
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spLoaiSanPham_get('%s');", MaLoaiSanPham);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {
                loaiSanPhamPOJO obj = new loaiSanPhamPOJO();
                obj.setMaNhom(rs.getString("MaNhom"));
                obj.setMaLoaiSanPham(rs.getString("MaLoaiSanPham"));
                obj.setTenLoaiSanPham(rs.getString("TenLoaiSanPham"));
                arr.add(obj);
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return arr;
    }
    public int insert(String MaLoaiSanPham, String TenLoaiSanPham, String MaNhom)
    {
        int result = 0;
        try{
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spLoaiSanPham_insert('%s','%s','%s');", MaLoaiSanPham,TenLoaiSanPham, MaNhom);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            if(st.execute()== false)
            {
                result = 1;
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return result;
    }
    public int update(String MaLoaiSanPham, String TenLoaiSanPham, String MaNhom)
    {
        int result = 0;
        try{
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spLoaiSanPham_update('%s','%s','%s');", MaLoaiSanPham,TenLoaiSanPham, MaNhom);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            if(st.execute()== false)
            {
                result = 1;
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return result;
    }
    public int delete(String MaLoaiSanPham)
    {
        int result = 0;
        try{
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spLoaiSanPham_delete('%s');", MaLoaiSanPham);
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            if(st.execute()== false)
            {
                result = 1;
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error
        }
        return result;
    }
    public String  issue()
    {
        String MaLoaiSanPhamMoi = "null";
        try
        {
            dbconnect con = new dbconnect();
            con.open();
            String sql = String.format("call spLoaiSanPham_issue();");
            CallableStatement st = (CallableStatement) con.getConn().prepareCall(sql);
            st.execute();
            ResultSet rs = st.getResultSet();
            while (rs.next())
            {                
                MaLoaiSanPhamMoi = rs.getString("MaLoaiSanPhamMoi");
            }
            con.close();
        }
        catch(SQLException ex)
        {
            //error;
        }
        return MaLoaiSanPhamMoi;
    }
}
